package structural.adapter;

import structural.adapter.custom.ClassAdapter;
import structural.adapter.custom.Entity;
import structural.adapter.custom.ObjectAdapter;
import structural.adapter.custom.spring.ModelAndView;
import structural.adapter.custom.struts.PersonAction;
import structural.adapter.custom.struts.StrutsAction;
import structural.adapter.java.ArrayIterator;

import java.util.Iterator;

/**
 * 适配器模式
 * 将一个类的接口转换成客户希望的另外一个接口，使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
 *
 * 与Converter 转换器不同
 */
public class Main {

    public static void main(String[] args) {
        // spring 请求转 struts 请求
        Entity reqEntity = new Entity();
        reqEntity.setId("1");
        reqEntity.setName("张三");


        System.out.println(" = = = object Adaptor");

        StrutsAction sc = new PersonAction();
        ObjectAdapter controllerAdapter = new ObjectAdapter(sc);
        ModelAndView view = controllerAdapter.add(reqEntity);
        System.out.println("view.name = " + view.getName());

        System.out.println(" = = = class Adaptor");

        ClassAdapter controllerAdapter2 = new ClassAdapter();
        ModelAndView view2 = controllerAdapter2.add(reqEntity);
        System.out.println("view.name = " + view2.getName());


        System.out.println(" = = = array Iterator");

        Object[] intArr = new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Iterator it = new ArrayIterator(intArr);
        while (it.hasNext()) {
            Object o = it.next();
            System.out.println(o);
            if (o == (Integer) 5) {
                it.remove();
            }
        }
        System.out.println(" 结果 ：");
        for (int i = 0; i < intArr.length; i++) {
            System.out.println(intArr[i]);
        }

    }
}
